package com.crm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.crm.entity.Asker;
import com.crm.entity.NetFollow;
import com.crm.service.NetFollowService;
import com.crm.service.StudentService;
import com.crm.util.Result;

//跟踪表的控制层
@RestController
@RequestMapping("/netFollow")
public class NetFollowController {
	@Autowired
	private NetFollowService netFollowService;// 自动注入
	@Autowired
	private StudentService studentService;// 自动注入

	/**
	 * 多条件查询(学员名称、跟踪者、开始时间、结束时间、回访情况、跟踪方式)
	 * 
	 * @param m
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getAllNetFollow")
	public Object getAllNetFollow(@RequestParam Map<String, Object> m, int page, int limit,
			String beginfollowTime,String endfollowTime,String begincreateTime,String endcreateTime,String loginName) {
		m.put("page", (page - 1) * limit);
		m.put("limit", limit);
		m.put("beginfollowTime", beginfollowTime);
		m.put("endfollowTime", endfollowTime);
		m.put("begincreateTime", begincreateTime);
		m.put("endcreateTime", endcreateTime);
		if (studentService.getLoginName(loginName) == 2) {// 是咨询师 根据咨询师去查询
			m.put("studentName", loginName);
			Result result = new Result("0", "true", netFollowService.getAllNetFollowCount(m),
					netFollowService.getAllNetFollow(m));
			return result;

		} else if (studentService.getLoginName(loginName) == 3) {// 是咨询经理，不传值
			Result result = new Result("0", "true", netFollowService.getAllNetFollowCount(m),
					netFollowService.getAllNetFollow(m));
			return result;

		}
		return null;
	}

	/**
	 * 新建跟踪信息
	 * 
	 * @param netFollow
	 * @return
	 */
	@RequestMapping("/addnetFollow")
	public Object addnetFollow(NetFollow netFollow) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		netFollow.setFollowTime(df.format(new Date()));
		netFollow.setCreateTime(df.format(new Date()));
		Result result = new Result("0", "true", netFollowService.addnetFollow(netFollow), "后台请求成功");
		return result;
	}
	/**
	 * 新建跟踪信息 学生表多选
	 * 
	 * @param netFollow
	 * @return
	 */
	@RequestMapping("/addnetFollowss")
	public Object addnetFollowss(NetFollow netFollow,@RequestParam("ids") Object ids,@RequestParam("askerids") int askerids) {
			JSONArray js = JSON.parseArray(ids.toString());//将从ids.toString()中获得的字符串直接转换成对象
			Integer jg = 0;
			for (Object i : js) {
				netFollow.setStudentId(Integer.parseInt(i.toString()));
				netFollow.setAskerId(askerids);//咨询师id是静态的
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
				netFollow.setFollowTime(df.format(new Date()));
				netFollow.setCreateTime(df.format(new Date()));
				netFollow.setFollowState("已跟踪");
				netFollow.setFollowTRype("家访");
				jg += netFollowService.addnetFollow(netFollow);
			}	
//				netFollow.setAskerId(Integer.parseInt(j.toString()));
			
			if (jg == js.size()) {
				return new Result("0", "添加成功", jg, "后台请求成功");
			}
		return new Result("0", "添加失败", 0, "false");
	}
	/**
	 * 新建跟踪信息 学生表
	 * 
	 * @param netFollow
	 * @return
	 */
	@RequestMapping("/addnetFollowsss")
	public Object addnetFollowssss(NetFollow netFollow) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		netFollow.setFollowTime(df.format(new Date()));
		netFollow.setCreateTime(df.format(new Date()));
		netFollow.setFollowState("已跟踪");
		Result result = new Result("0", "true", netFollowService.addnetFollow(netFollow), "后台请求成功");
		return result;
	}
	/**
	 * 查询今天签到的咨询师
	 * @return
	 */
	@RequestMapping("/selectUser")
	public Object selectUser(Asker asker) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		asker.setCheckInTime(df.format(new Date()));
		return netFollowService.selectUser(asker);
	}
	/**
	 * 查询没有被跟踪的学生
	 * @return
	 */
	@RequestMapping("/selectStudent")
	public Object selectStudent() {
		return netFollowService.selectStudent();
	}
	/**
	 * 根据学生id去查询追踪表 显示与对应学生关联的跟踪信息列表
	 * @return
	 */
	@RequestMapping("/selectStudentFollow")
	public Object selectStudentFollow(@RequestParam Map<String, Object> map) {
		Result result = new Result("0", "true",netFollowService.selectStudentFollowCount(map) ,netFollowService.selectStudentFollow(map));
		return result;
	}
}
